<?
class htmlwidget{
	public $type;
	public $html;
	public $attributes = array ();
	public $content;
	public $validAttribs = array(
		'name', 
		'value',
		'id', 
		'style', 
		'class', 
		'dir', 
		'lang', 
		'title',
		'onfocus', 
		'onblur', 
		'onclick', 
		'ondblclick', 
		'onmousedown', 
		'onmouseup', 
		'onmouseover', 
		'onmousemove', 
		'onmouseout', 
		'onkeypress', 
		'onkeydown', 
		'onkeyup'	
	);
	
	public function setname($name){
		$this->attributes['name'] = $name;
	}
	
	public function setvalue($value){
		$this->attributes['value'] = $value;
	}
	
	public function __construct($txt=''){
		if ( (strlen($txt)) || (! empty($txt)) ){
			$this->content = $txt;
		}
	}

	public function __toString(){
		if ( strlen($this->html)){
			return $this->html;
		} else {
			return '';
		}
	}

	public function getHtml(){
		return $this->__toString();
	}

	public function draw(){
		print $this->__toString();
	}

	public function setAttrib($key, $value){
		$key = strtolower($key);
		if ( in_array($key, $this->validAttribs)){
			$this->attributes[$key] = $value;
		} else {
			trigger_error("Invalid Attribute:" . $key . ' for element of type ' . get_class($this), E_USER_ERROR);
		}
	}

	public function clear(){
		$this->html = '';
		$this->attributes = array();
	}

	public function setContent($content){
		$this->content = $content;
	}
	
	public function appendContent($content){
		$this->content .= $content;
	}
	
	public function prependContent($content){
		$this->content = $content . $this->content;
	}

	public function randomstring($len=8) {
	    $chars = preg_split("//", 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', null, PREG_SPLIT_NO_EMPTY);
	    $string = '';
	    for ($i = 0; $i < $len; $i++) {
	        $string .= $chars[rand(0, count($chars) -1)];
	    }
	    return $string;
	}
}



class inputoption extends htmlwidget {
	public $displayvalue;
	public $checked = false;
	
	public function __construct(){
		parent::__construct();
		
		$this->validAttribs[] = 'onclick';
		$this->validAttribs[] = 'onfocus';
		$this->validAttribs[] = 'onchange';
		$this->validAttribs[] = 'value';
	}

	public function __toString(){
		if( ! in_array('id', array_keys($this->attributes))){
			$this->attributes['id'] = $this->randomstring(8);
		}
		$this->html = '<label for="' . $this->attributes['id'] . '">';
		$this->html .= '<input type="' . $this->type . '"';

		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		if ( $this->checked ){
			$this->html .= ' checked';
		}
		//add closing tag
		$this->html .=  ">";
		$this->html .= $this->displayvalue;
		$this->html .= '</label>';
		return $this->html;
	}

	public function check(){
		$this->checked = true;	
	}
	
	public function uncheck(){
		$this->checked = false;
	}

	public function setDisplayValue($value){
		$this->displayvalue = $value;
	}
}


class checkbox extends inputoption  {
	public function __construct(){
		parent::__construct();
		$this->type = 'checkbox';
	}
}

class radiobutton extends inputoption  {
	public function __construct(){
		parent::__construct();
		$this->type = 'radio';
	}
}


class inputset extends htmlwidget {
	public $values = array();
	public $width = 1;
	public $checkedvalue = '';
	public $type = 'checkbox'; //defaults to checkbox. could be radio too
	
	public function settype($type){
		if ( $type == 'checkbox' || $type == 'radio'){
			$this->type = $type;
		}
	}
	
	public function addValue($realvalue, $displayvalue=''){
		$this->values[$realvalue] = empty($displayvalue)? $realvalue : $displayvalue;
	}

	//how many options to display horizontally
	public function setWidth($width = 1){
		$this->width = $width;
	}

	public function setCheckedValue($value){
		$this->checkedvalue = $value;
	}

	
	public function __toString(){

		$table = new table();
		$temparr = array();
		foreach ( $this->values as $value => $displayvalue){
			$input = new $this->type();
			$input->setName($this->attributes['name']);
			$input->setValue($value);
			$input->setDisplayValue($displayvalue);
			if ( ($value == $this->checkedvalue) && (strlen($value)) ){
				$input->check();
			}
			$temparr[] = $input->getHtml();
			if( count($temparr) == $this->width){
				$table->row($temparr);
				$temparr = array();
			}
		}
		if( count($temparr)){
			$table->row($temparr);
		}
		return $table->getHtml();

	}

}





class checkboxset extends inputset {
	public function __construct(){
		parent::__construct();
		$this->type = 'checkbox';
	}
}


class radioset extends inputset {
	public function __construct(){
		parent::__construct();
		$this->type = 'radiobutton';
	}
}






///////////////////////////////////////////////////////////////////
////////////////////////select////////
///////////////////////////////////////////////////////////////////
class select extends htmlwidget {
		
	public $values = array();
	public $selected;
	public $defaultDisplay = '';
	public $usepresetvalue = true;
	
	public function __construct(){
		$GLOBALS[UD]->debug = array (1, get_class($this) . preg_replace("/^.+::/", "::", __METHOD__));
	
		parent::__construct();
		$this->validAttribs[] = 'size';
		$this->validAttribs[] = 'multiple';
		$this->validAttribs[] = 'onblur';
		$this->validAttribs[] = 'onchange';		
	}

	public function __toString(){
		$GLOBALS[UD]->debug = array (1, get_class($this) . preg_replace("/^.+::/", "::", __METHOD__));
	
		//opening tag
		$this->html = '<select';

		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		$this->html .= '>' . "\n";

		if ( strlen($this->defaultDisplay)){
			$this->html .= '<option value="">' . $this->defaultDisplay . '</option>' . "\n";
		}

		//options
		foreach ( $this->values as $value => $displayvalue){
			$selected = '';
			if ( strlen($this->selected)){
				if ( $value == $this->selected) {$selected = ' selected';}
			}
			if (! $this->usepresetvalue ){
				$selected = '';
			}
			$this->html .= '<option value="' . $value . '"'  . $selected . '>' . $displayvalue . '</option>' . "\n";
		}

		//closing tag
		$this->html .= "</select>\n";
		return $this->html;
	}
	
	public function setSelected($value){
		$this->selected = $value;
	}

	public function usepreset($value){
		$GLOBALS[UD]->debug = array (1, get_class($this) . preg_replace("/^.+::/", "::", __METHOD__));

		if ( $value ){
			$this->usepresetvalue = true;
		} else {
			$this->usepresetvalue = false;
		}
	}

	public function setDefaultDisplay($string){
		$GLOBALS[UD]->debug = array (1, get_class($this) . preg_replace("/^.+::/", "::", __METHOD__));

		$this->defaultDisplay = $string;
	}

	public function setValues($values){
		if ( is_array($values)){
			$this->values = $values;
		}
	}

	public function addValue($key, $value = ''){
		$this->values[$key] = (strlen($value))? $value:$key;
	}

}


///////////////////////////////////////////////////////////////////
////////////////////////img////////
///////////////////////////////////////////////////////////////////
class img extends htmlwidget {

	public function __construct($src = ''){
		$this->validAttribs[] = 'width';
		$this->validAttribs[] = 'height';
		$this->validAttribs[] = 'align';
		$this->validAttribs[] = 'valign';
		$this->validAttribs[] = 'border';
		$this->validAttribs[] = 'alt';
		$this->validAttribs[] = 'src';
		$this->validAttribs[] = 'hspace';
		$this->validAttribs[] = 'vspace';


		if ( (! empty($src)) || (strlen($src)) ){
			$this->attributes['src'] = $src;
		}
		$this->attributes['border'] = 0;
	}

	public function __toString(){
		//open tag
		$this->html = "<img";
		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		$this->html .= '>';
		return $this->html;
	}
}



///////////////////////////////////////////////////////////////////
////////////////////////list////////
///////////////////////////////////////////////////////////////////
class htmllist extends htmlwidget {
	public $items = array();

	public function __toString(){

		//opening tag
		$this->html = '<' . $this->type;

		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		$this->html .= '>' . "\n";

		//list items
		foreach ( $this->items as $item){
			if ( $item instanceof htmlwidget ){
				if ( $item instanceof ul || $item instanceof  ol){
					$this->html .= "\n\t" . '<li>' . "\n\t\t"  . $item->getHtml() . "\n\t\n" . '</li>' . "\n";
				} else {
					$this->html .= "\t" . '<li>' . $item . '</li>' . "\n";
				}
			} else {
				$this->html .= "\t" . '<li>' . $item . '</li>' . "\n";
			}
		}

		//closing tag
		$this->html .= "\n</" . $this->type . ">\n";
		return $this->html;
	}


	public function setItems($items){
		if ( is_array($items)){
			$this->items = $items;
		}
	}

	public function addItem($item){
		$this->items[] = $item;
	}

}

class ol extends htmllist {
	public function __construct(){
		$this->type = 'ol';
	}
}


class ul extends htmllist {
	public function __construct(){
		$this->type = 'ul';
	}
}




class form extends htmlwidget {
	public $elements = array();
	public $legends = array();
	public $tableAttribs = array();
	public $legendStyle;
	public $formTitle;
	public $hiddenElements = array();
	public $javascript;
	public $layouts = array('vertical', 'horizontal');
	public $layout = 'vertical';

	public function __construct(){
		//defaults
		$this->attributes['method'] = 'post';		
		$this->attributes['action'] = $_SERVER['SCRIPT_NAME'];
		
		$this->validAttribs[] = 'method';
		$this->validAttribs[] = 'action';
		$this->validAttribs[] = 'enctype';
		$this->validAttribs[] = 'onsubmit';
		$this->validAttribs[] = 'onreset';
	}
	
	public function setLayout($layout){
		if ( in_array($layout, $this->layouts)){
			$this->layout = $layout;
		}
	}

	public function setFormtitle($title){
		$title = new heading($title);
		$title->setAttrib('align', 'center');
		$this->formTitle = $title->getHtml();
	}

	public function addElement(&$element, $legend=""){
		if( $element instanceof hiddenbox ){
			$this->hiddenElements[] = $element;
		} else {
			$this->elements[] = $element;
			$this->legends[] = $legend;
		}
	}

	//set a class for the <td> that contain the legend of a field
	public function setLegendStyle($style){
		$this->legendStyle = $style;
	}

	public function setTableAttrib($key, $value){
		$this->tableAttribs[$key] = $value;
	}

	public function __toString(){

		//opening tag
		$this->html = '<form';
		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		$this->html .= ">\n";

		//hidden elements
		if ( count($this->hiddenElements)){
			foreach ( $this->hiddenElements as $elem){
				$this->html .= $elem->getHtml() . "\n";
			}
		}

		//form title
		$this->html .= $this->formTitle . "\n";	
		
		//the form content (in a table)
		$table = new table();		
		
		//first, the table attributes
		if ( is_array($this->tableAttribs) && count($this->tableAttribs)){
			foreach ( $this->tableAttribs as $key => $value){
				$table->setAttrib($key, $value);
			}
		}

		//then the form elements
		if ( $this->layout == 'vertical'){	
			foreach ( $this->elements as $key => $element){	
				
				if ( is_null($element)){
					$table->row($this->legends[$key], '');
				} elseif ( is_scalar($element) ){					
					if ((strlen($value) == 0) || empty($value) ){
						$table->row($this->legends[$key], '');
					} else {
						$table->row($this->legends[$key], $element);
					}
				} elseif ( $element instanceof htmlwidget ){
					$table->row($this->legends[$key], $element->getHtml());
				} elseif ( is_array($element) ){
					$table->row($this->legends[$key], join("", $element));
				} elseif ( is_object($element) && method_exists($element, '__toString') ){
					$table->row($this->legends[$key], $element->__toString());
				} else {
					$table->row($this->legends[$key], '');
				}
			
				if ( isset($this->legendStyle) && strlen($this->legendStyle)){
					$table->setCellAttrib('class', $this->legendStyle);
				}
			}
		} elseif ( $this->layout == 'horizontal'){
			$table->emptyrow();			
			foreach ( $this->elements as $key => $element){	
				
				if ( is_null($element)){
					$table->col($this->legends[$key]);
				} elseif ( is_scalar($element) ){					
					if ((strlen($value) == 0) || empty($value) ){
						$table->col($this->legends[$key]);
					} else {
						$table->col($this->legends[$key]);
					}
				} elseif ( $element instanceof htmlwidget ){
					$table->col($this->legends[$key]);
				} elseif ( is_array($element) ){
					$table->col($this->legends[$key]);
				} elseif ( is_object($element) && method_exists($element, '__toString') ){
					$table->col($this->legends[$key]);
				} else {
					$table->col($this->legends[$key]);
				}
				
				
				
			}
			$table->emptyrow();
			$table->setRowAttribs('valign', 'top');
			foreach ( $this->elements as $key => $element){	
				if ( is_null($element)){
					$table->col( '');
				} elseif ( is_scalar($element) ){					
					if ((strlen($value) == 0) || empty($value) ){
						$table->col('');
					} else {
						$table->col($element);
					}
				} elseif ( $element instanceof htmlwidget ){
					$table->col($element->getHtml());
				} elseif ( is_array($element) ){
					$table->col(join("", $element));
				} elseif ( is_object($element) && method_exists($element, '__toString') ){
					$table->col( $element->__toString());
				} else {
					$table->col('');
				}				
										
			}
		}
		
		$this->html .= $table->getHtml();

		//any javascript for the form
		if( ! empty($this->javascript)){
			$this->html .= '<script language="JavaScript" type="text/javascript">';
			$this->html .= $this->javascript;
			$this->html .= '</script>';
		}

		//closing tag
		$this->html .= "</form>\n";
		return $this->html;
	}

	public function addJavaScript($script){
		$this->javascript .= "\n\n" . $script;
	}

}


class htmlbox extends htmlwidget {
	
	public function __construct( $value = ''){
		if ( (strlen($value)) || ( ! empty($value))){
			$this->attributes['value'] = $value;
		}
	}
	
	public function __toString(){
		//open tag
		$this->html = '<input type="' . $this->type . '"';
		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}

		//add closing tag
		$this->html .=  ">";
		return $this->html;
	}
	
	public function setcontent($value){
		$this->attributes['value'] = $value;
	}
}

class textbox extends htmlbox {

	public function __construct( $value = ''){
		parent::__construct($value);
		$this->validAttribs[] = 'value';
		$this->validAttribs[] = 'size';
		$this->validAttribs[] = 'value';
		$this->validAttribs[] = 'maxlength';
		$this->type = 'textbox';
	}
}


class hiddenbox extends htmlbox {	
	public function __construct($value=''){
		parent::__construct($value);
		$this->type = 'hidden';
	}
}

class passwordbox extends htmlbox {
	
	public function __construct($value=''){
		parent::__construct($value);
		$this->type = 'password';
	}
}

class submit extends htmlbox {
	public function __construct( $value = ''){
		parent::__construct($value);
		$this->type = 'submit';
	}

}




///////////////////////////////////////////////////////////////////
////////////////////////table////////
///////////////////////////////////////////////////////////////////
class table extends htmlwidget {
	public $rows = array();
	public $cellattribs = array();
	public $rowattribs = array();
	public $colattribs = array();
	public $fullrows = array();
	public $numcolumns;
	public $numrows = 0;
	public $currentrownum = 0;
	public $currentcolnum = 0;
	public $caption = '';
	

	public function __construct(){
		$this->html = '';
		$this->numrows = 0;
		$this->numcolumns = 0;
		$this->validAttribs[] = 'width';
		$this->validAttribs[] = 'align';
		$this->validAttribs[] = 'valign';
		$this->validAttribs[] = 'background';
		$this->validAttribs[] = 'bgcolor';
		$this->validAttribs[] = 'border';
		$this->validAttribs[] = 'bordercolor';
		$this->validAttribs[] = 'bordercolordark';
		$this->validAttribs[] = 'bordercolorlight';
		$this->validAttribs[] = 'cellpadding';
		$this->validAttribs[] = 'cellspacing';
		$this->validAttribs[] = 'rules';
	}

	public function row(){

		$this->numrows++;
		$this->currentrownum++;
		$this->rows[$this->currentrownum] = array();

		$numcells = func_num_args();
		if ( $numcells > $this->numcolumns){
			$this->numcolumns = $numcells;
		}
		$cells = func_get_args();

		$arraysize = count($cells);
		foreach ( $cells as $key => $value){
			if ( (is_null($value))){
				$this->rows[$this->currentrownum][] = '';
			} else if ( is_scalar($value)){
				if ( strlen($value) > 0){
					$this->rows[$this->currentrownum][] = $value;
				} else {
					$this->rows[$this->currentrownum][] = '';
				}
			} elseif ( is_array($value)){
				$this->rows[$this->currentrownum][] = join(" ", $value);
			} elseif ( $value instanceof htmlwidget){
				$this->rows[$this->currentrownum][] = $value->getHtml();
			} elseif (is_object($value) && method_exists($value , '__toString')){
				$this->rows[$this->currentrownum][] = $value->__toString();
			} else {
				$this->rows[$this->currentrownum][] = '';
			}
		}
	}
	
	//adds a td that spans the whole row
	public function fullrow($value){

		$this->numrows++;
		$this->currentrownum++;
		$this->rows[$this->currentrownum] = array();

		$this->fullrows[] = $this->currentrownum;
		
		$numcells = 1;
		if ( $numcells > $this->numcolumns){
			$this->numcolumns = $numcells;
		}

		if ( is_null($value) ){
			$this->rows[$this->currentrownum][] = '';
		} else if ( is_scalar($value)){
			if ( strlen($value) == 0 ){
				$this->rows[$this->currentrownum][] = '';
			} else {
				$this->rows[$this->currentrownum][] = $value;
			}
		} elseif ( is_array($value)){
			$this->rows[$this->currentrownum][] = join(" ", $value);
		} elseif ( $value instanceof htmlwidget){
			$this->rows[$this->currentrownum][] = $value->getHtml();
		} elseif (is_object($value) && method_exists($value , '__toString')){
			$this->rows[$this->currentrownum][] = $value->__toString();
		} else {
			$this->rows[$this->currentrownum][] = '';
		}
	
	}
	
	public function emptyrow(){
		$this->numrows++;
		$this->currentrownum++;
		$this->rows[$this->currentrownum] = array();
	}
	
	public function col($value){
		if ( (is_null($value))){
			$this->rows[$this->currentrownum][] = '';
		} else if ( is_scalar($value)){
			if ((strlen($value) == 0) || empty($value) ){
				$this->rows[$this->currentrownum][] = '';
			} else {
				$this->rows[$this->currentrownum][] = $value;
			}
		} elseif ( is_array($value)){
			$this->rows[$this->currentrownum][] = join(" ", $value);
		} elseif ( $value instanceof htmlwidget){
			$this->rows[$this->currentrownum][] = $value->getHtml();
		} elseif (is_object($value) && method_exists($value , '__toString')){
			$this->rows[$this->currentrownum][] = $value->__toString();
		} else {
			$this->rows[$this->currentrownum][] = '';
		}	
	}
	
	public function setColAttrib($colnum, $name , $value){
		$this->colattribs[$colnum][$name] = $value;
	}

	public function setRowAttribs($attrname, $attrvalue,$rownum=0){
		if( ! $rownum || empty($rownum)){
			$rownum = $this->currentrownum;
		}
		$this->rowattribs[$rownum][$attrname] = $attrvalue;
	}

	public function setRowAttrib($attrname, $attrvalue,$rownum=0){
		$this->setRowAttribs($attrname, $attrvalue, $rownum);
	}
	
	public function setCellAttrib($attribname, $attribvalue, $column = '') {
		if (! $column || empty($column)){
			$column = $this->currentcolnum;
		}
		$this->cellattribs[$this->currentrownum][$column][$attribname] = $attribvalue;
	}

	public function zebra($color, $altcolor){
		for ( $i = 0; $i <= count($this->rows); $i++){
			if ( $i % 2 == 0){
				$this->setRowAttrib('bgcolor', $color, $i);
			} else {
				$this->setRowAttrib('bgcolor', $altcolor, $i);
			}
		}
	}	
	
	public function setCaption($caption){
		$this->caption = $caption;
	}
	
	public function __toString(){

		//add <table> tag
		$this->html = "\n<table";
		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		
		$this->html .= ">\n";

		if ( ! empty($this->caption)){
			$this->html .= '<caption>';
			$this->html .= '<em>'. $this->caption . '</em>';
			$this->html .= '</caption>';
		}
		
		//add all table rows
		foreach ( $this->rows as $rownum => $row){
			//opening <tr> tag
			$rowattr = '';
			if ( isset($this->rowattribs[$rownum])){
				if( is_array($this->rowattribs[$rownum] )){
					foreach ( $this->rowattribs[$rownum] as $k => $v){
						$rowattr .= $k . '="' . $v . '" ';
					}
				}
			}
			$rowattr = trim($rowattr);
			if ( strlen($rowattr)){
				$rowattr = ' ' . $rowattr;
			}
			$this->html .= "\t<tr" . $rowattr . ">\n";

			

			
			
			//<td> tags for this row
			$tdcount = 0;
			foreach ( $row as $cellnum => $cell){
				$attr = '';
				if ( isset($this->cellattribs[$rownum][$cellnum])){
					if ( is_array($this->cellattribs[$rownum][$cellnum]) ){
						foreach ( $this->cellattribs[$rownum][$cellnum] as $k => $v ) {
							$attr .= ' ' . $k . '="' . $v . '"';
						}
					}
				}

				if ( isset($this->colattribs[$tdcount])){
					if ( is_array($this->colattribs) ){
						foreach ( $this->colattribs[$tdcount] as $k => $v ) {
							$attr .= ' ' . $k . '="' . $v . '"';
						}
					}
				}
				
				if ( in_array( $rownum, $this->fullrows)){
					$attr .= ' colspan="' . $this->numcolumns . '"';				
				} 
				
				$attr = trim($attr);
				if ( strlen($attr)){
					$attr = ' ' . $attr;
				}
				$this->html .= "\t\t<td" . $attr . ">" . $cell . "</td>\n";
				$attr = '';
				$tdcount++;
			}
			
			if ( in_array( $rownum, $this->fullrows)){
				//closing tag and continue
				$this->html .= "\t</tr>\n";				
				continue;
			} 
			
			while( $this->numcolumns &&  ($tdcount < $this->numcolumns)){
				$this->html .= "\t\t<td>&nbsp;</td>\n";
				$tdcount++;
			}

			
			//closing </tr> tag
			$this->html .= "\t</tr>\n";
		}

		//add </table> tag
		$this->html .=  "</table>\n";
		return $this->html;
	}

	public function rowcount(){
		return $this->numrows;
	}
	
	public function clear(){
		parent::clear();
		$this->rowattribs = array();
		$this->cellattribs = array();
	}
}


///////////////////////////////////////////////////////////////////
////////////////////////html container type of widgets     ////////
///////////////////////////////////////////////////////////////////
class htmlcontainer extends htmlwidget {
	public $type = 'p';
	
	public function __construct($content = ''){
		parent::__construct();
		if ( (! empty($content)) || (strlen($content))){
			$this->content = $content;
		}
	}

	public function __toString(){
		//open tag
		$this->html = "<";
		
		//add html tag for this type of widget
		$this->html .= $this->type;
		
		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		$this->html .= '>';
		$this->html .= $this->content;
		//add closing tag
		$this->html .=  "</";
		$this->html .= $this->type;
		$this->html .= ">";
		return $this->html;
	}

	public function clear(){
		parent::clear();
		$this->content = '';
	}
}


class heading extends htmlcontainer {
	public $size=1;

	public function __construct($content = ''){
		parent::__construct($content);
		$this->validAttribs[] = 'align';
		$this->type = 'h1';		
	}

	public function setSize($size){
		$sizes = array(1, 2, 3, 4, 5, 6);
		if( in_array($size, $sizes)){
			$this->size = $size;
		} else {
			$size = 1;
		}
		$this->type = 'h' . $size;
	}

}

class anchor extends htmlcontainer {
	
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'a';
		
		$this->validAttribs[] = 'target';
		$this->validAttribs[] = 'shape';
		$this->validAttribs[] = 'tabindex';
		$this->validAttribs[] = 'accesskey';
		$this->validAttribs[] = 'lang';
		
		$this->attributes['href'] = '';
	}

	public function setHref($url){
		$this->attributes['href'] = $url;
	}
}



class span extends htmlcontainer {
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'span';
	}

}

class paragraph extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'p';
	}
}

class p extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'p';
	}
}

class em extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'em';
	}
}

class pre extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'pre';
	}
}

class code extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'code';
	}
}

class blockquote extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'blockquote';
	}
}
 
class q extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'q';
	}
}

class div extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'div';
	}
}

class strong extends htmlcontainer{
	public function __construct($content = ''){
		parent::__construct($content);
		$this->type = 'strong';
	}
}

class textarea extends htmlcontainer {
	public function __construct($content = ''){
		parent::__construct($content);
		$this->validAttribs[] = 'cols';
		$this->validAttribs[] = 'rows';
		$this->validAttribs[] = 'disabled';
		$this->validAttribs[] = 'readonly';
		$this->type = 'textarea';
	}
	
	public function setrows($numrows){
		$this->attributes['rows'] = $numrows;
	}
	
	public function setcols($numcols){
		$this->attributes['cols'] = $numcols;
	}	
}


/////////////////////////////////////////////////////////////
//////              Date Widgets                  ///////////
/////////////////////////////////////////////////////////////



class daywidget extends select {
	protected $validdays = array(28,29, 30, 31);
	public $numdays = 31;
	
	public function __construct($dayofmonth = ''){
		parent::__construct();
		if ( strlen($dayofmonth) && is_numeric($dayofmonth)){
			$this->setSelected($dayofmonth);
		}
	}

	public function __toString(){
		if ( ! in_array($this->numdays, $this->validdays)){
			$this->numdays =31;
		}
		for ( $i = 1; $i <= $this->numdays; $i++ ){
			$this->values[$i] = $i;
		}
		return parent::__toString();
	}

	public function setNumDays($num = ''){
		if( strlen($num) && in_array($num, $this->validdays)){
			$this->numdays = $num;
		}
	}
}

class monthwidget extends select {
	public $months = array(1,2,3,4,5,6,7,8,9,10,11,12);
	public $monthnames = array ('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september','october', 'november', 'december');
	public $daysinmonth = array (
		1 => 31,
		2 => 28,
		3 => 31,
		4 => 30,
		5 => 31,
		6 => 30,
		7 => 31,
		8 => 31,
		9 => 30,
		10 => 31,
		11 => 30,
		12 => 31
	);
	
	public $numdays;	
	public $displayFormat = 'Mon'; // Mon|mon|month|Month|Numeric
	public $month;
	
	public function __construct($month = ''){
		parent::__construct();
		if ( strlen($month)){
			if ( is_numeric($month)){
				if ( in_array($month, $this->months)){
					$this->month = $month;
				} else{
					trigger_error("The month {$month} supplied is invalid 1");
				}
			} elseif ( is_string($month)){
				$month = trim(strtolower($month));
				if ( in_array($month, $this->monthnames)){
					$this->month = array_search($month, $this->monthnames);
				} else {
					$found = 0;
					if ( strlen($month) == 3 ){
						foreach ( $this->monthnamess as $monthminus1 => $monthname ){
							$shortname = substr($monthname, 0, 3);
							if ( $shortname == $month){
								$this->month = $monthminus1 + 1;
								$found = 1;
								break;
							}
						}
					}
					if ( $found) {
						//ok
					} else {
						trigger_error("The month {$month} supplied is invalid 2");
					}
				}
			} else {
				trigger_error("The month {$month} supplied is invalid 3");
			}
		}
	}
	
	public function __toString(){
		foreach ( $this->monthnames as $num => $monthname){
			$fixednum = $num + 1;		
			if( $this->displayFormat == 'MON' ){
				$this->values[$fixednum] = substr(strtoupper($monthname), 0, 3);
			} elseif ( $this->displayFormat == 'Mon' ){
				$this->values[$fixednum] = substr(ucfirst($monthname), 0, 3);
			} elseif ( $this->displayFormat == 'mon' ){
				$this->values[$fixednum] = substr($monthname, 0, 3);				
			} elseif ( $this->displayFormat == 'Month' ){
				$this->values[$fixednum] = ucfirst($monthname);
			} elseif ( $this->displayFormat == 'month' ){
				$this->values[$fixednum] = $monthname;				
			} elseif (  $this->displayFormat == 'numeric' ){
				$this->values[$fixednum] = $fixednum;
			} else {
				$this->values[$fixednum] =substr($monthname, 0, 3);
			}
		}
		if ( in_array($this->month, $this->months)){
			$this->setSelected($this->month);
		}
		return parent::__toString();
	}
	
	public function setFormat($string){
		$validformats = array('Mon','mon', 'month', 'Month', 'Numeric', 'numeric', 'num');
		if ( in_array($string, $validformats)){
			$this->displayFormat = $string;
		}
	}

}


class yearwidget extends select {
	public $begin;
	public $end;
	
	public function __construct($year = ''){
		$curryear = date('Y');
		if ( strlen($year)){
			$this->selected = $year;
		}
		$this->begin = $curryear - 10;
		$this->end = $curryear + 10;
	}
	
	public function __toString(){
		for ( $i = $this->begin; $i <= $this->end; $i++ ){
			$this->values[$i] = $i;
		}
		return parent::__toString();
	}
	
	public function setBegin($year){
		$this->begin = $year;
	}
	
	public function setEnd($year){
		$this->end = $year;
	}	
	
	public function setRange($begin, $end){
		if ( $begin > $end ){
			$temp = $begin;
			$begin = $end;
			$end = $temp;
		}
		$this->setBegin($begin);
		$this->setEnd($end);
	}
}


class datedropdownwidget extends htmlwidget {
	public $year;
	public $month;
	public $day;
	
	public $yearwidget;
	public $monthwidget;
	public $daywidget;
	
	
	public $yeartitle = 'Year';
	public $monthtitle = 'Month';
	public $daytitle = 'Day';
	
	public $yearname = 'year';
	public $monthname = 'month';
	public $dayname = 'day';
	
	public $layout = 'vertical';//by default titles go above the drop down
	public $prefix = '';	
	public $suffix = '';
	public $randomstring = '';
	
	public $usepresetvalues = true;
	
	public function __construct( $day = '', $month = '',  $year = ''){
		$GLOBALS[UD]->debug = array (1, get_class($this) . preg_replace("/^.+::/", "::", __METHOD__));

		if ( empty($year)) $year = date('Y');
		if ( empty($month)) $month = date('n');
		if ( empty($day)) $day = date('j');
		
		if (! checkdate ( $month, $day, $year ) ){
			trigger_error('Invalid date: ' . $month . '/' . $day . '/' . $year);
		}
		
		$this->yearwidget  = new yearwidget($year);
		$this->monthwidget = new monthwidget($month);
		$this->daywidget   = new daywidget($day);
		$this->randomstring = $this->randomstring(5);
	}
	
	public function usepreset($value=true){
		$GLOBALS[UD]->debug = array (1, get_class($this) . preg_replace("/^.+::/", "::", __METHOD__));

		if ( $value ){
			$this->usepresetvalues = true;
		} else {
			$this->usepresetvalues = false;
		}
		if ( $this->usepresetvalues ){
			$this->daywidget->usepreset(true);
			$this->monthwidget->usepreset(true);
			$this->yearwidget->usepreset(true);
		} else {
			$this->daywidget->usepreset(false);
			$this->monthwidget->usepreset(false);
			$this->yearwidget->usepreset(false);			
		}
	}
	
	public function setlayout($layout){
		$layouts = array ('vertical', 'horizontal');
		if ( in_array($layout, $layouts)){
			$this->layout = $layout;
		}
	}
	
	public function __toString(){
		$GLOBALS[UD]->debug = array (1, get_class($this) . preg_replace("/^.+::/", "::", __METHOD__));
		$GLOBALS[UD]->ss = array($this->daywidget, 'daywidget');
		$this->setnames();
		$this->setCaptions();
		if ( $this->layout == 'vertical'){
			return $this->printvertical();
		} else {
			return $this->printhorizontal();
		}
	}
	
	public function printvertical(){
		$table =  new table();
		$table->row($this->monthtitle, $this->daytitle, $this->yeartitle);
		$table->row($this->monthwidget, $this->daywidget, $this->yearwidget);
		return $table->getHtml();
	}
	
	public function printhorizontal(){
		$table =  new table();
		$table->row($this->monthtitle, $this->monthwidget, $this->daytitle, $this->daywidget, $this->yeartitle, $this->yearwidget);
		return $table->getHtml();
	}
	
	public function setyearrange($begin, $end){
		$this->yearwidget->setRange($begin, $end);
	}
	
	public function setsuffix($suffix){
		$this->suffix = $suffix;
	}
	
	public function setprefix($prefix){
		$this->prefix = $prefix;
	}
	
	public function setcaptions(){
		$this->daywidget->setDefaultDisplay( $this->daytitle);
		$this->monthwidget->setDefaultDisplay( $this->monthtitle);
		$this->yearwidget->setDefaultDisplay( $this->yeartitle);
	}
	public function setnames(){
		$this->daywidget->setname  ($this->prefix . $this->dayname   . $this->suffix);
		$this->monthwidget->setname($this->prefix . $this->monthname . $this->suffix);
		$this->yearwidget->setname ($this->prefix . $this->yearname  . $this->suffix);
	}
	
	public function isleap($year){
		if ( $year % 4 == 0 ){
			if ( $year % 100 == 0 ){
				if ( $year % 400  == 0 ){
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}		
	}
}
?>
